package Guess;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @ClassName Main
 * @Description TODO
 * @Author WSY
 * @Date 2020-6-5 10:42
 */

public class Main {
    public static void main(String[] args) {
        WindowButton w = new WindowButton("猜");
    }
}

class WindowButton extends JFrame implements ActionListener {
    int number;
    int time1 = 30;
    JLabel hintLabel;
    JTextField inputGuess;
    JButton buttonGetNumber, buttonEnter;
    JLabel label, time;

    WindowButton(String s) {
        super(s);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });
        setLayout(new FlowLayout());
        buttonGetNumber = new JButton("得到一个随机数");
        add(buttonGetNumber);
        hintLabel = new JLabel("输入你的猜测：", JLabel.CENTER);
        hintLabel.setBackground(Color.cyan);
        inputGuess = new JTextField("0", 10);
        add(hintLabel);
        add(inputGuess);
        buttonEnter = new JButton("确定");
        add(buttonEnter);
        time = new JLabel("true");
        add(time, BorderLayout.CENTER);
        buttonEnter.addActionListener(this);
        buttonGetNumber.addActionListener(this);
        setBounds(500, 500, 750, 300);
        setVisible(true);
        validate();
    }

    long startTime = System.currentTimeMillis();

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == buttonGetNumber) {
            number = (int) (Math.random() * 100) + 1;
            new Thread(new MyThread()).start();
            hintLabel.setText("输入你的猜测：");
        } else if (e.getSource() == buttonEnter) {
            int guess = 0;
            time1 = 30;
            try {
                guess = Integer.parseInt(inputGuess.getText());
                if (guess == number) {
                    JOptionPane.showMessageDialog(buttonEnter, "猜对了，游戏结束！", "WIN!", JOptionPane.INFORMATION_MESSAGE);
                    System.exit(0);
                } else if (guess > number) {
                    hintLabel.setText("猜大了！");
                    inputGuess.setText(null);
                } else {
                    hintLabel.setText("猜小了！");
                    inputGuess.setText(null);
                }
            } catch (NumberFormatException event) {
                hintLabel.setText("请输入数字字符");
            }
        }
    }

    class MyThread implements Runnable {
        @Override
        public void run() {
            while (time1 > 0) {
                time1--;
                time.setText(time1 + "");
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            JOptionPane.showMessageDialog(time, "输入超时，游戏退出", "警告", JOptionPane.WARNING_MESSAGE);
            WindowButton.this.dispose();
        }
    }
}